package it.afs.worklog.application.form.admin;

import it.afs.worklog.Cliente;

import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;

/**
 * @author Fanta
 */
public class ClientiPage extends AdminPage
{
	public ClientiPage()
	{
		super();
		
		addComponents();
	}
	
	@Override
	protected void addComponents()
	{
		super.addComponents();
		
		add(getHomePageLink());
		
		LoadableDetachableModel<List<Cliente>> clientiModel = new LoadableDetachableModel<List<Cliente>>()
		{
			private static final long serialVersionUID = -272782806413296063L;

			@Override
			protected List<Cliente> load()
			{
				return getFactory().getDao().getClienti();
			}
		};
		
		ListView<Cliente> clienti = new ListView<Cliente>("clienti", clientiModel)
		{
			private static final long serialVersionUID = -3084858701816815340L;

			@Override
			protected void populateItem(ListItem<Cliente> item)
			{
				item.add(new Label("nominativo", new PropertyModel<String>(item.getModel(), "nominativo")));
				item.add(new Label("riferimenti", new PropertyModel<String>(item.getModel(), "riferimenti")));
				item.add(new WebMarkupContainer("attivo", new PropertyModel<Boolean>(item.getModel(), "attivo"))
				{
					private static final long serialVersionUID = 3634846791699540173L;

					@Override
					protected void onConfigure()
					{
						super.onConfigure();
						setVisible((Boolean)this.getDefaultModelObject());
					}
				});
				item.add(new Link<String>("edit", new PropertyModel<String>(item.getModel(), "id"))
				{
					private static final long serialVersionUID = 4588910754275392296L;

					@Override
					public void onClick()
					{
						setResponsePage(ClienteFormPage.class, new PageParameters("id=" + getModelObject()));
					}
				});
			}
		};
		add(clienti);
		
		add(new Link<Void>("add")
		{

			/**
			 * 
			 */
			private static final long serialVersionUID = -5046876066826971600L;

			@Override
			public void onClick()
			{
				setResponsePage(ClienteFormPage.class);
			}
		});
	}
}